/*
 * u_buf.h
 *
 *  Created on: 2012-10-5
 *      Author: lin
 */

#ifndef U_BUF_H_
#define U_BUF_H_

#include <stdarg.h>
#include "def/defs.h"

#define UBUF_OP_OK 0
#define UBUF_OP_ERR 1
#define UBUF_OP_OVERFLOW 2
typedef struct {
    uint32_t min;
    uint32_t max;
    uint32_t capacity;
    uint32_t len;

    char *cbuf;
} u_buf_t;

u_buf_t *u_buf_new(uint32_t min, uint32_t max);
static inline void u_buf_free(u_buf_t *a_buf) {
    zfree(a_buf->cbuf);
    zfree(a_buf);
}

int u_buf_extend(u_buf_t *a_buf, uint32_t extend);
int u_buf_truncate(u_buf_t *a_buf, uint32_t trun);
static inline int u_buf_truncate_min(u_buf_t *a_buf) {
    return u_buf_truncate(a_buf, a_buf->min);
}

int u_buf_append(u_buf_t *a_buf, const char *str, uint32_t strlen);
int u_buf_adjust_append(u_buf_t * a_buf, const char *str, uint32_t strlen,
        int left_adjust, uint32_t in_width, uint32_t out_width,
        char space_char);
int u_buf_vprintf(u_buf_t * a_buf, const char *format, va_list args);

int u_buf_printf_dec64(u_buf_t * a_buf, uint64_t ui64, int width);

static inline int u_buf_printf_dec32(u_buf_t * a_buf, uint32_t ui32, int width){
    return u_buf_printf_dec64(a_buf,ui32,width);
}

int u_buf_printf_hex(u_buf_t * a_buf, uint32_t ui32, int width);

#define u_buf_reset(buf) (buf)->len = 0
#define u_buf_capacity(buf) (buf)->capacity
#define u_buf_len(buf) (buf)->len
#define u_buf_min(buf) (buf)->min
#define u_buf_max(buf) (buf)->max

#endif /* U_BUF_H_ */
